package wang.dao;

import wang.bean.Theme;
import wang.utils.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class ThemeDao {
  public static final String FIELD_ID = "id";
  public static final String FIELD_NAME = "name";

  // 新增管理员
  public void insert(Theme theme) {
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
      // 获得数据的连接
      conn = JDBCUtils.getConnection();

      // 发送SQL语句
      String sql = "insert into t_theme(name) values(?)";

      ps = conn.prepareStatement(sql);
      ps.setString(1, theme.getName());
      int num = ps.executeUpdate();
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      JDBCUtils.release(rs, ps, conn);
    }
  }

  // 删除管理员
  public void delete(int id) {
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;
    try {
      conn = JDBCUtils.getConnection();
      stmt = conn.createStatement();
      String sql = "DELETE FROM t_theme WHERE id = " + id;
      int num = stmt.executeUpdate(sql);
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      JDBCUtils.release(rs, stmt, conn);
    }
  }

  // 修改管理员
  public boolean update(Theme theme) {
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
      conn = JDBCUtils.getConnection();
      String sql = "update t_theme set name = ? where id=?";
      ps = conn.prepareStatement(sql);
      ps.setString(1, theme.getName());
      ps.setInt(2, theme.getId());
      int num = ps.executeUpdate();
      return num > 0;
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      JDBCUtils.release(rs, ps, conn);
    }
    return false;
  }

  // 获取管理员
  public Theme get(int id) {
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;
    try {
      // 获得数据的连接
      conn = JDBCUtils.getConnection();
      // 获得Statement对象
      stmt = conn.createStatement();
      // 发送SQL语句
      String sql = "SELECT * FROM t_theme WHERE id = " + id;
      rs = stmt.executeQuery(sql);
      // 处理结果集
      while (rs.next()) {
        Theme theme = new Theme();
        theme.setId(rs.getInt(FIELD_ID));
        theme.setName(rs.getString(FIELD_NAME));
        return theme;
      }
      return null;
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      JDBCUtils.release(rs, stmt, conn);
    }
    return null;
  }

  public Theme findByName(String name) {
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;
    try {
      // 获得数据的连接
      conn = JDBCUtils.getConnection();
      // 获得Statement对象
      stmt = conn.createStatement();
      // 发送SQL语句
      String sql = "SELECT * FROM t_theme WHERE name = '" + name + "'";
      rs = stmt.executeQuery(sql);
      // 处理结果集
      while (rs.next()) {
        Theme theme = new Theme();
        theme.setId(rs.getInt(FIELD_ID));
        theme.setName(rs.getString(FIELD_NAME));
        return theme;
      }
      return null;
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      JDBCUtils.release(rs, stmt, conn);
    }
    return null;
  }

  public List<Theme> list() {
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;

    List<Theme> themes = new ArrayList<>();
    try {
      // 获得数据的连接
      conn = JDBCUtils.getConnection();
      // 获得Statement对象
      stmt = conn.createStatement();
      // 发送SQL语句
      String sql = "SELECT * FROM t_theme";
      rs = stmt.executeQuery(sql);
      // 处理结果集
      while (rs.next()) {
        Theme theme = new Theme();
        theme.setId(rs.getInt(FIELD_ID));
        theme.setName(rs.getString(FIELD_NAME));
        themes.add(theme);
      }
      return themes;
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      JDBCUtils.release(rs, stmt, conn);
    }
    return new ArrayList<>();
  }
}
